<template>
  <div class="mnt-dialog form-container">
    <el-form ref="form" :model="formModel" label-width="160px" :rules="rules">
      <el-row style="text-align: center">
        <el-col :span="12" >
          <el-form-item label="所属模块:" prop="tmBasModuleId">
            <el-select v-model="formModel.tmBasModuleId" :disabled="disabledModule" clearable filterable>
              <el-option v-for="item in moduleOptions" :key="item.tmBasModuleId" :label="item.moduleCode+'-'+item.moduleDesc" :value="item.tmBasModuleId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段代码:" prop="filedCode">
            <el-input v-model="formModel.filedCode" :disabled="disabledFiled" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段描述:" prop="filedDesc">
            <el-input v-model="formModel.filedDesc" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="字段类型:" prop="filedType">
            <hey-select v-model="formModel.filedType" :disabled="disabledFiledType" clcd="Key_ECU_Module_Filed" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row class="submit-area">
      <el-col :span="24">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="handleSubmit()">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  name: 'ParamConfigMnt',
  props: ['pageType', 'editData'],
  data() {
    return {
      disabledModule:false,
      disabledFiled:false,
      disabledFiledType:false,
      moduleOptions:[],
      formModel: {
        trBasModuleFiledId: '',
        tmBasModuleId: '',
        filedCode: '',
        filedType: '',
        filedDesc: '',
      },
      rules: {
      }
    }
  },
  computed: {

  },
  mounted() {
    this.getModule()
    this.init()
  },
  methods: {
    getModule(){
      const param = {pageNum:1,pageSize:9999}
      param.isDelete = '0'
      this.get('/tmBasModule/queryTmBasModule', param).then(res => {
        this.moduleOptions = res.list
      })
    },
    init() {
      this.$form = this.$refs.form
      const { pageType, editData, formModel } = this
      if (pageType !== 'add') {
        this.assignData(editData, formModel)
        this.disabledModule = true
        this.disabledFiled = true
        this.disabledFiledType = true
      }
      this.initRules()
    },
    initRules() {
      const REQUIRED_MSG = this.CONST.REQUIRED_MSG
      this.rules = {
        tmBasModuleId: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedCode: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedType: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
        filedDesc: [{ required: true, message: REQUIRED_MSG, trigger: ['change', 'blur'] }],
      }
      this.$nextTick(_ => {
        this.$form.clearValidate()
      })
    },
    handleSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { pageType, formModel } = this
          const param = { ...formModel }
          const operateType = pageType === 'edit' ? '02' : '01'
          param.operateType = operateType
          const url = '/trBasModuleFiled/maintainTrBasModuleFiled'
          const tip = pageType === 'edit' ? '修改成功！' : '新增成功！'
          this.post(url, param).then(res => {
            this.$info(tip, () => {
              this.$emit('mntSuc', res)
              this.closeDialog()
            })
          })
        }
      })
    },
    closeDialog() {
      this.$parent.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
